<?php

	// +----------------------------------------------------------------------
	// | 店铺处理
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\store;

	use app\common\event\Base;
	use app\event\file\QrCode;
	use app\pay\event\UserBalance as UserBalanceEvent;
	use app\pay\model\Payment as PaymentModel;
	use app\event\store\StoreCreditDeposit as StoreCreditDepositEvent;
	use app\model\ucenter\User as UserModel;
	use app\model\store\Store as StoreModel;
	use app\event\ucenter\User as UserEvent;
	use think\facade\Db;
	use think\Exception;
	use think\facade\Config;
	use think\facade\Env;
	use think\facade\Session;
	use think\facade\Url;

	class Address extends Base
	{

        /**
         * 通用更新方法
         * @param array   $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2020/6/30 17:31
         */
        public function update($param, $user = null)
        {
            try {
                if (empty($param)) {
                    throw new Exception(Lang::get('__data_format_error_'));
                }
                if (!empty($param['id'])) {
                    // 更新操作
                    $address = $user->addresses()->where('id', $param['id'])->find();
                    if (empty($address)) {
                        throw new Exception('地址不存在');
                    }
                    $address['name'] = $param['name'];
                    $address['mobile'] = $param['mobile'];
                    $address['address'] = $param['address'];
                    $address['is_default'] = $param['is_default'];
                    $address['province_name'] = $param['province_name'];
                    $address['city_name'] = $param['city_name'];
                    $address['country_name'] = $param['country_name'];
                    $address['province_id'] = $param['province_id'];
                    $address['city_id'] = $param['city_id'];
                    $address['country_id'] = $param['country_id'];
                    $result = $address->save();
                } else { // 新增
                    $address['name'] = $param['name'];
                    $address['mobile'] = $param['mobile'];
                    $address['address'] = $param['address'];
                    $address['is_default'] = $param['is_default'];
                    $address['province_name'] = $param['province_name'];
                    $address['city_name'] = $param['city_name'];
                    $address['country_name'] = $param['country_name'];
                    $address['province_id'] = $param['province_id'];
                    $address['city_id'] = $param['city_id'];
                    $address['country_id'] = $param['country_id'];
                    $result = $user->addresses()->save($address);
                }
                $data['code'] = 0;
                $data['data'] = $result;
                $data['message'] = '操作成功';
            } catch (\Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }



    }
